﻿//======================================================
//==     (c)2008 aspxcms inc by NeTCMS v1.0              ==
//==          Forum:bbs.aspxcms.com                   ==
//==         Website:www.aspxcms.com                  ==
//======================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NetCMS.Content;
using NetCMS.Model;

public partial class manage_channel_value : NetCMS.Web.UI.ManagePage
{
    public manage_channel_value()
    {
        this.Authority_Code = "D014";
    }
    Channel rd = new Channel();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.CheckAdminAuthority();
        this.PageNavigator1.OnPageChange += new PageChangeHandler(PageNavigator1_PageChange);
        if (!IsPostBack)
        {
            copyright.InnerHtml = CopyRight;
            Response.CacheControl = "no-cache";                        //设置页面无缓存
            string ChID = Request.QueryString["ChID"];
            string Action = Request.QueryString["Action"];
            if (SiteID.Trim() != "0")
            {
                PageError("您没权限管理字段", "javascript:history.back();", true);
            }
            if (ChID == string.Empty || ChID == null)
            {
                PageError("错误的参数", "javascript:history.back()", true);
            }
            else
            {
                if (Action != string.Empty && Action != null)
                {
                    string gId = Request.QueryString["Id"];
                    switch (Action)
                    {
                        case "del":
                            string Datastr = rd.getChannelTable(int.Parse(ChID.ToString()));
                            if (Datastr == "#")
                            {
                                PageError("错误的参数，找不到数据库表。", "javascript:history.back();", true);
                            }
                            delFields(int.Parse(gId.ToString()), Datastr);
                            break;
                        case "Lock":
                            LockStat(int.Parse(gId.ToString()), 1);
                            break;
                        case "unLock":
                            LockStat(int.Parse(gId.ToString()), 0);
                            break;
                    }
                }
                StartLoad(1);
            }
        }
    }

    protected void delFields(int ID,string DataTable)
    {
        rd.delFileds(ID, DataTable);
        PageRight("删除字段成功.", "javascript:history.back();", true);
    }

    protected void LockStat(int ID,int Num)
    {
        rd.updateValueFileds(ID, Num);
        if (Num == 1)
        {
            PageRight("锁定字段成功.", "javascript:history.back();", true);
        }
        else
        {
            PageRight("取消锁定字段成功.", "javascript:history.back();", true);
        }
    }

    /// <summary>
    /// PageNavigator1_PageChange 的摘要说明
    /// 分页加载函数
    /// </summary>
    protected void PageNavigator1_PageChange(object sender, int PageIndex)
    {
        StartLoad(PageIndex);
    }
    protected void StartLoad(int PageIndex)
    {
        int i, j;
        DataTable dt = null;
        SQLConditionInfo st = new SQLConditionInfo("@ChID", int.Parse(Request.QueryString["ChID"]));
        dt = NetCMS.Content.Pagination.GetPage(this.GetType().Name, PageIndex, 20, out i, out j, st);
        this.PageNavigator1.PageCount = j;
        this.PageNavigator1.PageIndex = PageIndex;
        this.PageNavigator1.RecordCount = i;
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Columns.Add("op", typeof(string));
            dt.Columns.Add("FieldName", typeof(string));
            dt.Columns.Add("ChannelName", typeof(string));
            dt.Columns.Add("FieldType", typeof(string));
            dt.Columns.Add("isUserStr", typeof(string));
            string ChanName = string.Empty;
            IDataReader dr = rd.getChInfoMenu(int.Parse(Request.QueryString["ChID"].ToString()));
            if (dr.Read())
            {
                ChanName = dr["channelName"].ToString();
            }
            dr.Close();
            for (int k = 0; dt.Rows.Count > k; k++)
            {
                string LockStr = "";
                if (dt.Rows[k]["isLock"].ToString() == "1")
                {
                    LockStr = "<a href=\"value.aspx?Action=unLock&Id=" + dt.Rows[k]["ID"].ToString() + "&ChID=" + dt.Rows[k]["ChID"].ToString() + "\" class=\"list_link\" title=\"点击开放此字段\"  onClick=\"{if(confirm('确定要开放此字段吗？')){return true;}return false;}\">锁定</a>";
                }
                else
                {
                    LockStr = "<a href=\"value.aspx?Action=Lock&Id=" + dt.Rows[k]["ID"].ToString() + "&ChID=" + dt.Rows[k]["ChID"].ToString() + "\" class=\"list_link\" title=\"点击锁定此字段\"  onClick=\"{if(confirm('确定要锁定此字段吗？')){return true;}return false;}\">开放</a>";
                }
                dt.Rows[k]["op"] = "<a href=\"value_add.aspx?vID=" + dt.Rows[k]["ID"].ToString() + "&ChID=" + dt.Rows[k]["ChID"].ToString() + "\" class=\"list_link\">修改</a>&nbsp;┊&nbsp;<a href=\"value.aspx?Action=del&Id=" + dt.Rows[k]["ID"].ToString() + "&ChID=" + dt.Rows[k]["ChID"].ToString() + "\" class=\"list_link\" onClick=\"{if(confirm('确定要删除吗？')){return true;}return false;}\">删除</a>&nbsp;┊&nbsp;" + LockStr + "";
                dt.Rows[k]["FieldName"] = "<a href=\"value_add.aspx?ChID=" + dt.Rows[k]["ChID"].ToString() + "&vID=" + dt.Rows[k]["ID"].ToString() + "\" class=\"list_link\">" + dt.Rows[k]["CName"].ToString() + "</a>";
                dt.Rows[k]["ChannelName"] = ChanName;
                dt.Rows[k]["FieldType"] = getvType(int.Parse(dt.Rows[k]["vType"].ToString()), dt.Rows[k]["isNulls"].ToString(), int.Parse(dt.Rows[k]["isSearch"].ToString()));
                if (dt.Rows[k]["isUser"].ToString() == "1")
                {
                    dt.Rows[k]["isUserStr"] = "<span class=\"reshow\">是</span>";
                }
                else
                {
                    dt.Rows[k]["isUserStr"] = "否";
                }
            }
        } 
        Fieldslists.DataSource = dt;                              //设置datalist数据源
        Fieldslists.DataBind();                                   //绑定数据源

    }

    protected string getvType(int vType,string isNulls,int isSearch)
    {
        string LenStr = string.Empty;
        string NullStr = " <span style=\"font-size:10px;\">(NOT NULL)</span>";
        if (isNulls == "1")
        {
            NullStr = " <span class=\"reshow\" style=\"font-size:10px;\">(NULL)</span>";
        }
        string SearchStr = "(无编辑器)";
        switch (isSearch)
        {
            case 1:
                SearchStr = "(简易编辑器)";
                break;
            case 2:
                SearchStr = "(标准编辑器)";
                break;
            case 3:
                SearchStr = "(完整编辑器)";
                break;
            default:
                SearchStr = "(无编辑器)";
                break;
        }
        switch (vType)
        {
            case 0:
                LenStr = "单行文本:nvarchar(20)" + NullStr;
                break;
            case 1:
                LenStr = "单行文本:nvarchar(50)" + NullStr;
                break;
            case 2:
                LenStr = "单行文本:nvarchar(100)" + NullStr;
                break;
            case 3:
                LenStr = "单行文本:nvarchar(180)" + NullStr;
                break;
            case 4:
                LenStr = "单行文本:nvarchar(225)" + NullStr;
                break;
            case 5:
                LenStr = "备注型:ntext(16)" + NullStr;
                break;
            case 6:
                LenStr = "下拉菜单:nvarchar(200)" + NullStr;
                break;
            case 7:
                LenStr = "数字类型:int(4)" + NullStr;
                break;
            case 8:
                LenStr = "数字类型:tinyint(1)" + NullStr;
                break;
            case 9:
                LenStr = "货币类型:Money(8)" + NullStr;
                break;
            case 10:
                LenStr = "长日期类型:DateTime(8)" + NullStr;
                break;
            case 11:
                LenStr = "短日期类型:SmallDatetime(4)" + NullStr;
                break;
            case 12:
                LenStr = "单选框:nvarchar(200)" + NullStr;
                break;
            case 13:
                LenStr = "单选框:ntext(16)" + NullStr;
                break;
            case 14:
                LenStr = "复选框:nvarchar(200)" + NullStr;
                break;
            case 15:
                LenStr = "复选框:ntext(16)" + NullStr;
                break;
            case 16:
                LenStr = "多行列表框:ntext(16)" + NullStr;
                break;
            case 17:
                LenStr = "备注型:ntext(16)" + NullStr + SearchStr;
                break;
            default:
                LenStr = "NetCMS自定义类型，文本:nvarchar(200)" + NullStr;
                break;
        }
        return LenStr;
    }
    protected void DataList1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }

}
